﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace VietImage.Models
{
    public partial class LogUser
    {
        #region Properties
        public User ActiveUser
        {
            get
            {
                return this.GetActiveUser();
            }
        }

        public User PassiveUser
        {
            get
            {
                return this.GetPassiveUser();
            }
        }

        public Action Action
        {
            get
            {
                return this.GetAction();
            }
        }
        #endregion

        #region Methods
        public User GetActiveUser()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Users.Where(p => p.ID == this.ID_ActiveUser);
            if (v.Count() == 0)
                return null;
            return v.First();
        }

        public User GetPassiveUser()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Users.Where(p => p.ID == this.ID_PassiveUser);
            if (v.Count() == 0)
                return null;
            return v.First();
        }

        public Action GetAction()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Actions.Where(p => p.ID == this.ID_Action);
            if (v.Count() == 0)
                return null;
            return v.First();
        }

        public List<ErrorItem> Rollback(int id_User, int id_Lang)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            String[] lContent = this.Content.Split('_');

            int errorMess = 0;
            try
            {

                if (lContent[0].Contains("[Detail]"))
                {
                    DetailUser di = new DetailUser();
                    di.ID_User = this.ID_PassiveUser;
                    di.ID_Language = int.Parse(lContent[1]);
                    di.Firstname = lContent[2];
                    di.Lastname = lContent[3];
                    di.Address = lContent[4];

                    errorList = DetailUser.ChangeInformation(di, id_User, this.ID, id_Lang);
                }
                else
                {
                    User i = new Models.User();
                    i.ID = this.ID_PassiveUser;
                    i.Username = lContent[0];
                    i.Password = lContent[1];
                    i.ID_Status = int.Parse(lContent[2]);
                    i.Birthday = DateTime.Parse(lContent[3]);
                    i.ID_City = int.Parse(lContent[4]);
                    i.Email = lContent[5];
                    i.Cellfone = lContent[6];
                    i.Gender = int.Parse(lContent[7]);
                    i.ID_Permission = int.Parse(lContent[8]);

                    errorList = User.ChangeInformation(i, id_User, id_Lang);
                }
            }
            catch
            {
                errorMess = 48;
            }

            if (errorMess != 0)
                if(errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));

            return errorList;
        }
        #endregion

        #region Static Methods
        public static List<LogUser> GetList()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            return _db.LogUsers.OrderByDescending(p=>p.ActionTime).ToList();
        }
        #endregion
    }
}